@RestController
@RequestMapping("/api/v1/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    @GetMapping("/records")
    public void exportRecords(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "EXCEL") ExportType exportType,
            @RequestParam(required = false) Double minTemp,
            @RequestParam(required = false) Double maxTemp,
            @RequestParam(required = false) List<String> columns,
            HttpServletResponse response) {

        // 设置响应头
        response.setContentType(exportType.getContentType());
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + generateFileName(exportType) + "\"");

        // 执行导出
        try (OutputStream out = response.getOutputStream()) {
            exportService.exportTemperatureRecords(
                    new ExportCriteria(startDate, endDate, minTemp, maxTemp, columns),
                    exportType,
                    out
            );
        } catch (IOException e) {
            throw new ExportException("导出失败: " + e.getMessage());
        }
    }

    private String generateFileName(ExportType type) {
        return "temperature_records_" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) 
                + type.getExtension();
    }
}